<template>
  <div class="discussion-detail-container" v-if="discussion">
    <div class="discussion-header">
      <h1 class="discussion-title">{{ discussion.title }}</h1>
      <div class="discussion-meta">
        <div class="discussion-author">
          <el-avatar :size="40" :src="discussion.author.avatar"></el-avatar>
          <div class="author-info">
            <div class="author-name">{{ discussion.author.name }}</div>
            <div class="post-time">发表于 {{ discussion.createTime }}</div>
          </div>
        </div>
        <div class="discussion-actions">
          <el-button 
            :type="isFavorite ? 'danger' : 'default'" 
            size="small" 
            @click="toggleFavorite"
          >
            <el-icon><Star /></el-icon>
            {{ isFavorite ? '已收藏' : '收藏' }}
          </el-button>
        </div>
      </div>
      <div class="discussion-tags">
        <el-tag
          v-for="tag in discussion.tags"
          :key="tag"
          size="small"
          effect="plain"
          class="tag"
        >{{ tag }}</el-tag>
      </div>
    </div>

    <div class="discussion-content">
      <p>{{ discussion.content }}</p>
    </div>

    <div class="discussion-stats">
      <span class="views"><el-icon><View /></el-icon> {{ discussion.views }} 浏览</span>
      <span class="likes"><el-icon><Star /></el-icon> {{ discussion.likes }} 点赞</span>
      <span class="comments"><el-icon><ChatDotRound /></el-icon> {{ discussion.comments.length }} 评论</span>
    </div>

    <div class="discussion-comments">
      <h3>评论 ({{ discussion.comments.length }})</h3>
      
      <div class="comment-list">
        <div v-for="comment in discussion.comments" :key="comment.id" class="comment-item">
          <div class="comment-author">
            <el-avatar :size="32" :src="comment.author.avatar"></el-avatar>
            <div class="author-info">
              <div class="author-name">{{ comment.author.name }}</div>
              <div class="comment-time">{{ comment.createTime }}</div>
            </div>
          </div>
          <div class="comment-content">
            <p>{{ comment.content }}</p>
          </div>
          <div class="comment-actions">
            <el-button type="text" size="small">
              <el-icon><ThumbUp /></el-icon> {{ comment.likes }}
            </el-button>
            <el-button type="text" size="small" @click="replyToComment(comment)">
              <el-icon><ChatDotRound /></el-icon> 回复
            </el-button>
          </div>
        </div>
      </div>
      
      <div class="comment-form">
        <h4>发表评论</h4>
        <el-form @submit.prevent="submitComment">
          <el-form-item>
            <el-input
              v-model="commentContent"
              type="textarea"
              :rows="4"
              placeholder="写下你的评论..."
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitComment" :loading="submitting">
              发表评论
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
  <div v-else class="loading-container">
    <el-skeleton :rows="10" animated />
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { useStore } from 'vuex';
import { useRoute, useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { View, ChatDotRound, Star, ThumbUp } from '@element-plus/icons-vue';

const store = useStore();
const route = useRoute();
const router = useRouter();

const discussionId = computed(() => Number(route.params.id));
const discussion = computed(() => store.getters['community/currentDiscussion']);
const isFavorite = computed(() => store.getters['community/isDiscussionFavorite'](discussionId.value));

const commentContent = ref('');
const submitting = ref(false);

// 获取讨论详情
onMounted(async () => {
  try {
    await store.dispatch('community/getDiscussionDetail', discussionId.value);
  } catch (error) {
    ElMessage.error('获取讨论详情失败');
    console.error(error);
  }
});

// 收藏/取消收藏
const toggleFavorite = async () => {
  try {
    await store.dispatch('community/toggleFavoriteDiscussion', discussionId.value);
    ElMessage.success(isFavorite.value ? '已取消收藏' : '收藏成功');
  } catch (error) {
    ElMessage.error('操作失败');
    console.error(error);
  }
};

// 回复评论
const replyToComment = (comment) => {
  commentContent.value = `@${comment.author.name} `;
  // 滚动到评论框
  document.querySelector('.comment-form').scrollIntoView({ behavior: 'smooth' });
};

// 提交评论
const submitComment = async () => {
  if (!commentContent.value.trim()) {
    ElMessage.warning('评论内容不能为空');
    return;
  }
  
  submitting.value = true;
  try {
    await store.dispatch('community/addDiscussionComment', {
      discussionId: discussionId.value,
      content: commentContent.value
    });
    
    commentContent.value = '';
    ElMessage.success('评论发表成功');
  } catch (error) {
    ElMessage.error('评论发表失败');
    console.error(error);
  } finally {
    submitting.value = false;
  }
};
</script>

<style lang="scss" scoped>
.discussion-detail-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.discussion-header {
  margin-bottom: 20px;
  
  .discussion-title {
    font-size: 24px;
    margin-bottom: 15px;
  }
  
  .discussion-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    
    .discussion-author {
      display: flex;
      align-items: center;
      gap: 10px;
      
      .author-info {
        .author-name {
          font-weight: bold;
        }
        
        .post-time {
          font-size: 12px;
          color: #909399;
        }
      }
    }
  }
  
  .discussion-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
    margin-bottom: 15px;
  }
}

.discussion-content {
  margin-bottom: 20px;
  line-height: 1.6;
  
  p {
    margin-bottom: 15px;
  }
}

.discussion-stats {
  display: flex;
  gap: 20px;
  color: #909399;
  margin-bottom: 20px;
  padding-bottom: 20px;
  border-bottom: 1px solid #ebeef5;
  
  .views,
  .likes,
  .comments {
    display: flex;
    align-items: center;
    gap: 5px;
  }
}

.discussion-comments {
  h3 {
    font-size: 18px;
    margin-bottom: 20px;
  }
  
  .comment-list {
    margin-bottom: 30px;
    
    .comment-item {
      padding: 15px 0;
      border-bottom: 1px solid #ebeef5;
      
      &:last-child {
        border-bottom: none;
      }
      
      .comment-author {
        display: flex;
        align-items: center;
        gap: 10px;
        margin-bottom: 10px;
        
        .author-info {
          .author-name {
            font-weight: bold;
          }
          
          .comment-time {
            font-size: 12px;
            color: #909399;
          }
        }
      }
      
      .comment-content {
        margin-bottom: 10px;
        line-height: 1.5;
      }
      
      .comment-actions {
        display: flex;
        gap: 15px;
      }
    }
  }
  
  .comment-form {
    h4 {
      font-size: 16px;
      margin-bottom: 15px;
    }
  }
}

.loading-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}
</style> 